package Presentacion.Principal.UsuarioLogin;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;

import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.SwingConstants;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import Fisica.Configuracion.ConfProperties;
import Fisica.Configuracion.Configuracion;
import Logica.BaseDeDatos.ABMUsuarioLogica;
import Modelo.Usuarios;
import Presentacion.BaseDeDatos.ABMUsuarios.ABMUsuarios;
import Presentacion.Principal.Main;
import Presentacion.Principal.MainMarket;

import java.awt.Component;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPasswordField;

public class UsuarioLogin extends JFrame {

	private JPanel contentPane;
	private JTextField tfApellidoYNombre;
	private JPasswordField tfClave1;
	private JButton btnNuevoUsuario;

	private List<Usuarios>list;
	
	Fisica.Configuracion.Configuracion conf;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UsuarioLogin frame = new UsuarioLogin();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UsuarioLogin() {
		
		setResizable(false);
		setTitle("Iniciar sesi\u00F3n");
		this.setVisible(false);

		/**Pregunto si al presionar la "X" esta seguro de que quiere cerrar la ventana.*/
		this.addWindowListener(new WindowAdapter() {
	           public void windowClosing(WindowEvent e) {
	        	   setDefaultCloseOperation(0); 
	        	   cerrarVentana();
	           }
	       });
		
		/***/
		int width = 330;
        int height = 170;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width)/2;
        int y = (screen.height - height)/2;
        setBounds( x, y, width, height);
	
        contentPane = new JPanel();
		contentPane.setBackground(new Color(192, 192, 192));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblApellidoYNombre = new JLabel("Usuario");
		lblApellidoYNombre.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblApellidoYNombre.setBounds(10, 10, 80, 27);
		contentPane.add(lblApellidoYNombre);
		
		tfApellidoYNombre = new JTextField();
		tfApellidoYNombre.setHorizontalAlignment(SwingConstants.CENTER);
		tfApellidoYNombre.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent a) {
				if (a.getKeyCode() == KeyEvent.VK_ENTER) {
					aceptar();
				}
			}
		});
		tfApellidoYNombre.addFocusListener(new FocusAdapter() {
			
			public void focusGained(FocusEvent arg0) {
				pintaVerde(tfApellidoYNombre);
			}
			
			public void focusLost(FocusEvent arg0) {
				pintaBlanco(tfApellidoYNombre);
			}
		});
		tfApellidoYNombre.setBounds(85, 10, 200, 25);
		contentPane.add(tfApellidoYNombre);
		tfApellidoYNombre.setColumns(10);
		tfApellidoYNombre.requestFocus();
		
		JLabel lblClave1 = new JLabel("Clave");
		lblClave1.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblClave1.setBounds(10, 48, 70, 27);
		contentPane.add(lblClave1);
		
		tfClave1 = new JPasswordField();
		tfClave1.setHorizontalAlignment(SwingConstants.CENTER);
		tfClave1.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent a) {
				if (a.getKeyCode() == KeyEvent.VK_ENTER) {
					aceptar();
				}
			}
		});
		tfClave1.addFocusListener(new FocusAdapter() {
			
			public void focusGained(FocusEvent arg0) {
				pintaVerde(tfClave1);
			}
			
			public void focusLost(FocusEvent arg0) {
				pintaBlanco(tfClave1);
			}
		});
		tfClave1.setColumns(10);
		tfClave1.setBounds(85, 48, 100, 25);
		contentPane.add(tfClave1);
		
		btnNuevoUsuario = new JButton("Nuevo Usuario");
		btnNuevoUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		btnNuevoUsuario.setFont(new Font("Tahoma", Font.PLAIN, 14));
		btnNuevoUsuario.setBounds(10, 86, 280, 30);
		contentPane.add(btnNuevoUsuario);
		
		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				aceptar();
			}
		});
		btnAceptar.setBounds(202, 48, 89, 23);
		contentPane.add(btnAceptar);
		
		contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{tfApellidoYNombre, tfClave1, btnAceptar, btnNuevoUsuario}));
		
		this.list = ABMUsuarioLogica.getList();
		
		this.setFoco();
//		cargoConfUsuario();
	}
	
	/*Metodo para confirma si esta seguro que quiere cerrar la aplicacinn*/	
	public void cerrarVentana(){
			this.setVisible(false);					
	}
	
//  ** Pinto JTEXTFIELD de color   **  // 
	public void pintaVerde(JTextField tf){
		tf.setBackground(new Color(153, 255, 153));
	}
	
	public void pintaBlanco(JTextField tf){
		tf.setBackground(new Color(255, 255, 255));
	}
	
	public void aceptar(){
		try {
			Usuarios u;
			String user, password;
			user = tfApellidoYNombre.getText().toUpperCase();
			password = tfClave1.getText().toUpperCase();
			Iterator<Usuarios> it = list.iterator();
			while(it.hasNext()){
				u = it.next();
				if (u.getNombre().equalsIgnoreCase(user) && u.getClave().equalsIgnoreCase(password))
				{
					new MainMarket();
					Fisica.Configuracion.Configuracion.escribirUltimoUsuario(user);
					this.dispose();
					return;
				}
				
			}
			JOptionPane.showMessageDialog(this, "No se encontro el USUARIO", "ERROR", JOptionPane.ERROR_MESSAGE);
			tfClave1.setText("");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/** 				LEVANTO NUMERO DE TERMINAL Y PUNTO DE VENTA 				*/
	public boolean cargoConfUsuario(){
		String url = "/Properties/VentanaLogin.properties";
		ConfProperties cp = new ConfProperties();
		String user = cp.getProperties(url, "user").toUpperCase();
		if (user.length() <= 0){
			
			this.dispose();
//			this.m.crearConfiguracion();
			return false;
		}
		tfApellidoYNombre.setText(user);
		tfClave1.requestFocus();
		return true;
	}
	
	public void setFoco(){
		if(tfApellidoYNombre.getText().length() > 0){
			tfClave1.requestFocus();
		}
	}
}
